package cn.newgrand.netcall.consultant.ui;

import org.jivesoftware.resource.Res;
import org.jivesoftware.spark.PluginManager;
import org.jivesoftware.spark.util.ResourceUtils;
import org.jivesoftware.spark.util.SwingWorker;
import cn.newgrand.netcall.consultant.SearchManager;
import cn.newgrand.smackx.consultant.entity.SearchSpecialistEntity;
import cn.newgrand.netcall.consultant.ConsultantPlugin;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.List;

public class SpecialistSearchForm extends JPanel {
    private SpecialistSearchResults searchResults;
    private FormPanel questionForm;
    private SearchManager searchManager;

    public SpecialistSearchForm() {

        //searchManager = new UserSearchManager(SparkManager.getConnection());
    	searchManager = ((ConsultantPlugin)PluginManager.getInstance().getPlugin(ConsultantPlugin.class)).getSearchManager();
        setLayout(new GridBagLayout());

        // Load searchForm

//        try {
//            searchForm = searchManager.getSearchForm(service);
//        }
//        catch (XMPPException e) {
//            Log.error("Unable to load search services.", e);
//            JOptionPane.showMessageDialog(SparkManager.getMainWindow(), Res.getString("message.search.service.not.available"), Res.getString("title.notification"), JOptionPane.ERROR_MESSAGE);
//            return;
//        }

        //searchManager = new UserSearchManager(SparkManager.getConnection());
        questionForm = new FormPanel();
        questionForm.setBorder(BorderFactory.createTitledBorder(Res.getString("group.search.form")));

        add(questionForm, new GridBagConstraints(0, 0, 3, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));

        // Add User DataForm
        final JButton searchButton = new JButton();
        ResourceUtils.resButton(searchButton, Res.getString("button.search"));
        add(searchButton, new GridBagConstraints(0, 1, 3, 1, 1.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));

        searchButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                performSearch();
            }
        });

        KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
        String enterString = org.jivesoftware.spark.util.StringUtils.keyStroke2String(enter);

        // Handle Left Arrow
        getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(enterString), "enter");

        getActionMap().put("enter", new AbstractAction("enter") {
            public void actionPerformed(ActionEvent evt) {
                performSearch();
            }
        });

        // Add searchResults
        searchResults = new SpecialistSearchResults();
        searchResults.setBorder(BorderFactory.createTitledBorder(Res.getString("group.search.results")));
        add(searchResults, new GridBagConstraints(0, 2, 2, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
    }

    public FormPanel getQuestionForm() {
        return questionForm;
    }

//    public Form getSearchForm() {
//        return searchForm;
//    }

    /**
     * Starts a search based on the Answered form.
     */
    public void performSearch() {
        searchResults.clearTable();

        SwingWorker worker = new SwingWorker() {
 //           ReportedData data;
        	List<SearchSpecialistEntity> data;
            public Object construct() {
//                try {
//                    Form answerForm = questionForm.getFilledForm();
//                    data = searchManager.getSearchResults(answerForm, serviceName);
            	 data = searchManager.searchSpecialistEntity(questionForm.getSearchType(), questionForm.getField1(), questionForm.getField2(), questionForm.getField3());
//                }
//                catch (XMPPException e) {
//                    Log.error("Unable to load search service.", e);
//                }

                return data;
            }

            public void finished() {
                if (data != null) {
                    searchResults.showUsersFound(data);
                    searchResults.invalidate();
                    searchResults.validate();
                    searchResults.repaint();
                }
                else {
                    JOptionPane.showMessageDialog(searchResults, Res.getString("message.no.results.found"), Res.getString("title.notification"), JOptionPane.ERROR_MESSAGE);
                }
            }
        };

        worker.start();


    }
}
